<?php

namespace app\model;

class OrderApprovalModel extends BaseModel
{
    protected $name = 'dingdan_approval';

    public const STATUS_ING = 200; //审批中
    public const STATUS_SU = 300; //成功
    public const STATUS_FAIL = 400; //驳回
    public const STATUS_CANCEL = 500; //撤销


    /**
     * Notes: 检测审批流程是否通过
     * User: chenLei
     * Date: 9/14/25
     * @param $order_approval_id
     * @return false|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkAllApprovalsPassed($order_approval_id)
    {
        $info = $this->where('id', $order_approval_id)->findOrEmpty();
        if ($info->isEmpty()) {
            $this->error = '审批流程不存在-checkAllApprovalsPassed';
            return false;
        }
        if ($info->status != self::STATUS_ING) {
            $this->error = '审批流程已结束-checkAllApprovalsPassed';
            return false;
        }
        $order = OrderModel::where('id', $info->dingdan_id)->findOrEmpty();
        if ($order->isEmpty()) {
            $this->error = '订单信息异常-checkAllApprovalsPassed';
            return false;
        }
        if ($order->dingdan_status != OrderModel::STATUS_ING) {
            $this->error = '该订单状态不属于审批中-checkAllApprovalsPassed';
            return false;
        }


        $approval_user_model = new OrderApprovalUserModel();
        $approval_users = $approval_user_model->where('dingdan_approval_id', $order_approval_id)->select();

        $isPass = true;
        foreach ($approval_users as $value) {
            if ($value['status'] != OrderApprovalUserModel::APPROVED) {
                $isPass = false;
            }
        }

        if ($isPass) {
            $info->status = self::STATUS_SU;
            $info->save();

            $order->dingdan_status = OrderModel::STATUS_SUCCESS;
            $order->save();
        } else {
            OrderApprovalUserModel::where('dingdan_approval_id', $order_approval_id)->order('id asc')
                ->limit(1)
                ->where('status', OrderApprovalUserModel::PENDING)
                ->update(['status' => OrderApprovalUserModel::APPROVING]);
        }


        return true;

    }


    /**
     * Notes: 拒绝
     * User: chenLei
     * Date: 9/14/25
     * @param $order_approval_id
     * @return bool
     */
    public function rejectApprovalProcess($order_approval_id)
    {
        $info = $this->where('id', $order_approval_id)->findOrEmpty();
        if ($info->isEmpty()) {
            $this->error = '审批流程不存在-rejectApprovalProcess';
            return false;
        }
        if ($info->status != self::STATUS_ING) {
            $this->error = '审批流程已结束-rejectApprovalProcess';
            return false;
        }
        $order = OrderModel::where('id', $info->dingdan_id)->findOrEmpty();
        if ($order->isEmpty()) {
            $this->error = '订单信息异常-rejectApprovalProcess';
            return false;
        }
        if ($order->dingdan_status != OrderModel::STATUS_ING) {
            $this->error = '该订单状态不属于审批中-rejectApprovalProcess';
            return false;
        }


        $info->status = self::STATUS_FAIL;
        $info->save();

        $order->dingdan_status = OrderModel::STATUS_FAIL;
        $order->save();

        return true;
    }
}